package com.stephenone.customviewdemo

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.View

class DashView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private val TAG = DashView::class.java.simpleName
    private val paint = Paint().apply {
        isAntiAlias = true
        strokeWidth = 3f.dp
        style = Paint.Style.STROKE
    }

    val radius = 150f.dp
    val sweepAngel = 260f


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawArc(width / 2f - radius,height / 2f - radius,width / 2f + radius,height / 2f + radius,140f,sweepAngel,false,paint)
        //旋转画布方法
//        canvas.save()
//
//        canvas.translate(width / 2f,height / 2f)
//        var currentAngle = 140f
//        canvas.rotate(140f)
//        //减去一半strokeWidth所造成的突出
//        canvas.drawLine(radius - 10f.dp,3f.dp/2,radius,3f.dp/2,paint)
//        for (index in 1..19){
//            Log.e(TAG, "onDraw: $currentAngle")
//            canvas.rotate(sweepAngel/20)
//            canvas.drawLine(radius - 10f.dp,0f,radius,0f,paint)
//        }
//        canvas.rotate(sweepAngel/20)
//        canvas.drawLine(radius - 10f.dp, (-3f).dp /2,radius, (-3f).dp /2,paint)
//
//        canvas.restore()


        //PathDashPathEffect 方法

        //每个刻度的path
        val scale = Path()
        scale.addRect(0f,0f,3f.dp,10f.dp,Path.Direction.CW)

        val length = Path()
        length.addArc(width / 2f - radius,height / 2f - radius,width / 2f + radius,height / 2f + radius,140f,sweepAngel)
        val pathMeasure = PathMeasure(length,false)
        //相当于画虚线
        paint.pathEffect = PathDashPathEffect(scale,(pathMeasure.length - paint.strokeWidth) / 20,0f,PathDashPathEffect.Style.ROTATE)
        canvas.drawArc(width / 2f - radius,height / 2f - radius,width / 2f + radius,height / 2f + radius,140f,sweepAngel,false,paint)
        paint.pathEffect = null

        //画指针
        canvas.save()
        canvas.translate(width / 2f,height / 2f)
        canvas.rotate(140f + sweepAngel/20 * 4)
        canvas.drawLine(0f,0f,radius * 3 / 4,0f,paint)
        canvas.restore()
    }
}